package com.gzk.hrm.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gzk.hrm.constants.RabbitMQConstants;
import com.gzk.hrm.context.LoginContext;
import com.gzk.hrm.domain.*;
import com.gzk.hrm.dto.CourseAddDto;
import com.gzk.hrm.feign.CourseElasticSearchFeign;
import com.gzk.hrm.mapper.CourseDetailMapper;
import com.gzk.hrm.mapper.CourseMapper;
import com.gzk.hrm.mapper.CourseMarketMapper;
import com.gzk.hrm.service.ICourseService;
import com.gzk.hrm.service.ICourseTypeService;
import com.gzk.hrm.to.EmailMessageTo;
import com.gzk.hrm.to.SmsMessageTo;
import com.gzk.hrm.to.SystemMessageTo;
import com.gzk.hrm.util.AjaxResult;
import com.gzk.hrm.util.AssertUtils;
import com.gzk.hrm.doc.CourseDoc;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jake.Chen
 * @since 2022-02-12
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMarketMapper courseMarketMapper;

    @Autowired
    private CourseDetailMapper courseDetailMapper;

    @Autowired
    private ICourseTypeService courseTypeService;

    @Autowired
    private CourseElasticSearchFeign courseElasticSearchFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void save(CourseAddDto dto) {
        Course course = dto.getCourse();
        CourseDetail courseDetail = dto.getCourseDetail();
        CourseMarket courseMarket = dto.getCourseMarket();

        course.setStatus(0);
        course.setSaleCount(0);
        course.setCommentCount(0);
        course.setViewCount(0);

        //设置课程的用户信息和机构信息
        Login login = LoginContext.getLogin();
        course.setUserId(login.getId());
        course.setUserName(login.getUsername());
        course.setTenantId(login.getTenantId());
        course.setTenantName(login.getTenantName());

        //保存课程
        insert(course);
        //保存营销
        courseMarket.setId(course.getId());
        courseMarketMapper.insert(courseMarket);

        //保存详情
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);

        //修改课程类型总量的数量
        CourseType courseType = courseTypeService.selectById(course.getCourseTypeId());

        courseType.setTotalCount(courseType.getTotalCount()+1);

        courseTypeService.updateById(courseType);
    }

    @Override
    @Transactional
    public void onLineCourse(Long id) {
        //1.判断参数
        AssertUtils.isNotNull(id,"参数不合法！");
        //2.查数据库，判断状态是否为下线
        Course course = selectById(id);

        AssertUtils.isNotNull(course, "参数不合法，课程不存在！");
        AssertUtils.isEquals(course.getStatus(), Course.STATUS_OFFLINE, "该课程已经上线了哦");
        //3.修改状态，存入数据库
        course.setStatus(Course.STATUS_ONLINE);
        //设置上线时间
        course.setOnlineTime(new Date());
        updateById(course);
        //4.生成CourseDoc，用feign调用search服务把课程信息发送到ES

        //根据课程id查询营销
        CourseMarket courseMarket = courseMarketMapper.selectById(course.getId());
        CourseDoc courseDoc = new CourseDoc();
        //使用BeanUtils工具拷贝属性
        BeanUtils.copyProperties(courseMarket, courseDoc);
        BeanUtils.copyProperties(course, courseDoc);
        courseDoc.setPrice(courseMarket.getPrice().doubleValue());
        courseDoc.setPriceOld(courseMarket.getPriceOld().doubleValue());
        //feign调用search服务把课程信息发送到ES
        AjaxResult ajaxResult = courseElasticSearchFeign.saveCourse(courseDoc);

        AssertUtils.isTrue(ajaxResult.isSuccess(), "上传失败！！ES瓦了");
        publishMessage(course);
    }

    @Override
    public void offLineCourse(Long id) {
        //1.判断参数
        AssertUtils.isNotNull(id,"参数不合法！");
        //2.查数据库，判断状态是否为上线
        Course course = selectById(id);
        AssertUtils.isNotNull(course, "参数不合法，课程不存在！");
        AssertUtils.isEquals(course.getStatus(), Course.STATUS_ONLINE, "该课程已经下线了哦");

        //3.修改状态，存入数据库
        course.setStatus(Course.STATUS_OFFLINE);
        //设置下线时间
        course.setOfflineTime(new Date());
        updateById(course);

        //调用search服务，从ES中删除课程
        AjaxResult ajaxResult = courseElasticSearchFeign.deleteCourse(course.getId());

        AssertUtils.isTrue(ajaxResult.isSuccess(), "删除失败！！ES瓦了");

    }

    public void publishMessage(Course course){
        String content = "号外号外，课程[%s]已经发布了，<a href='http://www.baidu.com?courseId=%s'>点击链接</a>查看详情";
        content = String.format(content, course.getName(), course.getId());

        //发送邮件
        rabbitTemplate.convertAndSend(RabbitMQConstants.EXCHANGE_NAME_TOPIC, "course.email", new EmailMessageTo(Arrays.asList("23534@qq.com","234726@qq.com"),"新课程上线了",content));

        //发送短信
        rabbitTemplate.convertAndSend(RabbitMQConstants.EXCHANGE_NAME_TOPIC, "course.sms", new SmsMessageTo(Arrays.asList("1874356934","1438957344"),content));

        //发送站内信
        rabbitTemplate.convertAndSend(RabbitMQConstants.EXCHANGE_NAME_TOPIC, "course.smsg", new SystemMessageTo(Arrays.asList(1874356934l,1438957344l),"新课程上线啦",content));

    }
}
